<html>
<head><title>CS143 Project 1B</title></head>
<body>
Type an SQL query in the following box:
<p>
<form action="." method="GET">
<textarea name="query" cols="60" rows="8">
<?php if(isset($_GET['query'])) print $_GET['query']; ?>
</textarea>
<input type="submit" value="Submit" />
</form>
</p>
<p><small>Note: tables and fields are case sensitive. Run "show tables" to see the list of
available tables.</small>
</p>

<?php
if(isset($_GET['query']))
{
	$db_connection = mysql_connect("localhost", "cs143", "");
	if(!$db_connection) {
		$errmsg = mysql_error($db_connection);
		print "Connection failed: " . $errmsg . "<br />";
		exit(1);
	}

	mysql_select_db("CS143", $db_connection);

	$query = $_GET['query'];
	$rs = mysql_query($query, $db_connection);
	if(!$rs) {
		$errmsg = mysql_error($rs);
		print "Connection failed: " . $errmsg . "<br />";
		exit(1);
	}
	
	print "<h3>Results from SQL:</h3>";
	print "<table border='1'>";
	
	print "<tr>";
	while($field = mysql_fetch_field($rs))
	{
		print "<td align='center'><strong>" . $field->name . "</strong></td>";
	}
	print "</tr>";
	
	while($row = mysql_fetch_row($rs)) {
		print "<tr>";
		foreach($row as $col)
		{
			if($col == NULL)
				print "<td align='center'>N/A</td>";
			else
				print "<td align='center'>" . $col . "</td>";
		}
		print "</tr>";
	}
	
	print "</table>";

	mysql_close($db_connection);
}
?>

</body>
</html>
